package com.imooc.chatroom;

import org.springframework.boot.web.servlet.ServletContextInitializer;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;

@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer, ServletContextInitializer {

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        registry.addHandler(new ChatHandler(), "/chat");
    }

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        String bufferSize = Integer.toString(1 << 20);

        servletContext.setInitParameter("org.apache.tomcat.websocket.textBufferSize",bufferSize);
        servletContext.setInitParameter("org.apache.tomcat.websocket.binaryBufferSize",bufferSize);
    }
}
